package com.qf.java2102.qihaonan.hotel.controller.verificationCode;

import cn.dsna.util.images.ValidateCode;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author qhn
 * @version v1.0
 * @date 2021/05/20
 **/
@WebServlet("/VerificationCode")
public class VerificationCode extends HttpServlet {
    //生成验证码
    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置验证码图片形状
        ValidateCode validateCode=new ValidateCode(200,30,4,20);
        //拿到创建好的输入字符字符串
        String code = validateCode.getCode();
        //获得session
        HttpSession session=request.getSession();
        //把验证码放到作用域中
        session.setAttribute("code",code);
        //响应给客户端
        validateCode.write(response.getOutputStream());
    }

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}